package cms.simmytech.service;

import cms.simmytech.mapper.TbContentMapper;
import cms.simmytech.mapper.TbMajorMapper;
import cms.simmytech.mapper.TbUserMajorMapper;
import cms.simmytech.mapper.TbUserMapper;
import cms.simmytech.model.*;
import cms.simmytech.response.BaseResponse;
import cms.simmytech.util.Constants;
import cms.simmytech.util.ResultCode;
import cms.simmytech.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommonService {

    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private TbUserMajorMapper userMajorMapper;
    @Autowired
    private TbMajorMapper majorMapper;

    protected BaseResponse generateUserCode(TbUser user) {
        BaseResponse response = new BaseResponse(ResultCode.INVALID_PARAMETER);
        try {
            if (!StringUtils.isEmpty(user.getUserCode())){
                response.setStat(ResultCode.SUCCESS);
                response.setMess(user.getUserCode());
                return response;
            }

            TbUserMajorExample userMajorExample = new TbUserMajorExample();
            userMajorExample.createCriteria().andUserIdEqualTo(user.getUserId()).andMajorStateEqualTo(Constants.ONE);
            userMajorExample.setOrderByClause("major_type asc,create_time asc");
            PageHelper.startPage(1,1);
            List<TbUserMajor> userMajors = userMajorMapper.selectByExample(userMajorExample);
            if (userMajors.isEmpty())
                return response;
            TbUserMajor userMajor = userMajors.get(0);
            TbMajor major = majorMapper.selectByPrimaryKey(userMajor.getMajorId());
            if (null == major)
                return response;
            //学院编号(2位):01信息学院;
            StringBuilder userCodeStringBuilder = new StringBuilder("01");
            //学位编号(1位):1.本科;2.硕士;3.博士;4.专科
            userCodeStringBuilder.append(userMajor.getMajorType());
            //学科编号(2位):01.本科计算机;
            userCodeStringBuilder.append(major.getBack5());
            //入学年份(4位):1999
            userCodeStringBuilder.append(userMajor.getEnterYear());
            //查询id信息
            PageHelper.startPage(1,1);
            TbUserExample userExample = new TbUserExample();
            userExample.createCriteria().andUserCodeLike(userCodeStringBuilder.toString() + "%");
            userExample.setOrderByClause("user_code desc");
            List<TbUser> users = userMapper.selectByExample(userExample);
            if (users.isEmpty()) {
                userCodeStringBuilder.append("0001");
                response.setMess(userCodeStringBuilder.toString());
            } else {
                TbUser lastUser = users.get(0);
                if (lastUser.getUserId().intValue()!=user.getUserId()){
                    String lastUserCode = lastUser.getUserCode();
                    long lastUserCodeLong = Long.valueOf(lastUserCode);
                    long userCode = lastUserCodeLong + 1;
                    response.setMess(userCode + "");
                }else{
                    response.setMess(lastUser.getUserCode());
                }
            }
            response.setStat(ResultCode.SUCCESS);

        } catch (
                Exception ex) {
            ex.printStackTrace();
        }
        return response;
    }


}
